package com.yonyou.pmclouds.siteinspection.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.annotation.Insert;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.AdditionalPropertiesVOWrapper;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.service.CommonPropertiesProcessor;
import com.yonyou.pmclouds.enterprise.entity.EnterpriseInfo;
import com.yonyou.pmclouds.inspectitem.entity.InspectItemVO;
import com.yonyou.pmclouds.inspectitem.rmiitf.InspectItemService;
import com.yonyou.pmclouds.outter.enterprise.EnterpriseInfoQuery;
import com.yonyou.pmclouds.outter.user.UserInfoQuery;
import com.yonyou.pmclouds.siteinspection.entity.SiteInspectionListViewVO;
import com.yonyou.pmclouds.siteinspection.entity.SiteInspectionVO;
import com.yonyou.pmclouds.siteinspection.rmiitf.SiteInspectionMaintain;
import com.yonyou.pmclouds.siteinspection.rmiitf.SiteInspectionQuery;
import com.yonyou.pmclouds.user.entity.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Slf4j
@Controller
//@RequestMapping(value = "/mobile/siteinspection")
@RequestMapping(value = "/siteinspection")
public class SiteInspectionController {
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private SiteInspectionMaintain maintain;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private SiteInspectionQuery query;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private InspectItemService inspectItemService;
    @Autowired
    private UserInfoQuery userQuery;
    @Autowired
    private EnterpriseInfoQuery constructionunitQuery;
    @Autowired
    private CommonPropertiesProcessor propertiesProcessor;
    private static final String[] USER_ID_FIELDS = {SiteInspectionVO.CREATOR, InspectItemVO.PK_RECTIFICATIONER};
    private static final String[] USER_NAME_FIELDS = {SiteInspectionVO.CREATOR_NAME, InspectItemVO.RECTIFICATIONER_NAME};

    @Insert
    @RequestMapping(value = "/add")
    @ResponseBody
    public SiteInspectionVO add( @RequestBody SiteInspectionVO vo) throws BusinessException {

        vo.setCreator(RuntimeEnvironment.getLoginUserId());
        setUserInfo(new SiteInspectionVO[]{vo});
        SiteInspectionVO add = maintain.add(vo);
        return add;
    }

    @RequestMapping(value = "/queryByProject")
    @ResponseBody
    public AdditionalPropertiesVOWrapper[] queryByProject(@RequestParam("pk_project") String pk_project,
                                                          @RequestParam(value = "billType", defaultValue = BillTypeConst.SITEINSPECTION_BILLTYPE) String billType) throws BusinessException {
        SiteInspectionListViewVO[] vos = query.queryByBillTypeProject(pk_project, billType);
        if (vos == null || vos.length == 0) {
            return new AdditionalPropertiesVOWrapper[0];
        }

        userQuery.tranUserInfoField(vos, USER_ID_FIELDS, USER_NAME_FIELDS);
        return propertiesProcessor.wrapAdditionalProps(vos);
    }

    @RequestMapping(value = "/getDetail")
    @ResponseBody
    public SiteInspectionVO queryByPk(String pk_siteinspection) throws BusinessException {
        SiteInspectionVO siteInspectionVO = query.queryOne(pk_siteinspection);
        userQuery.tranUserInfoField(new SiteInspectionVO[]{siteInspectionVO}, USER_ID_FIELDS, USER_NAME_FIELDS);
        // 施工单位
        String pk_constructionunit = siteInspectionVO.getPk_constructionunit();
        if (!StringUtils.isEmpty(pk_constructionunit)) {
            EnterpriseInfo enterpriseInfo = constructionunitQuery.queryEnterpriseById(pk_constructionunit);
            siteInspectionVO.setConstruction_name(enterpriseInfo.getName());
        }
        return siteInspectionVO;

    }

    //检查用到的工具类

    @RequestMapping(value = "/getInspectItemDetail")
    @ResponseBody
    public InspectItemVO queryInspectItemVO(String pk_inspectitem) throws BusinessException {
        InspectItemVO vo = inspectItemService.queryOne(pk_inspectitem);
        userQuery.tranUserInfoField(new InspectItemVO[]{vo}, new String[]{InspectItemVO.PK_RECTIFICATIONER}, new String[]{InspectItemVO.RECTIFICATIONER_NAME});
        return vo;
    }

    //==============================分界线========================================================================================


//    @RequestMapping(value = "/getPics")
//    @ResponseBody
//    public String[] getPics(HttpServletResponse response,String pk_siteinspection) throws BusinessException {
//        response.setHeader("Access-Control-Allow-Origin", "*");
//        return query.queryPics(pk_siteinspection);
//    }

    private void setUserInfo(SiteInspectionVO[] siteInspectionVOS){
        if(siteInspectionVOS == null || siteInspectionVOS.length <= 0){
            return;
        }

        Set<String> userIdSet = new HashSet<>();

        for(SiteInspectionVO siteInspectionVO : siteInspectionVOS){

            // 整改人名称转换
            InspectItemVO[] inspectItemVOS = siteInspectionVO.getInspectitems();
            if(inspectItemVOS != null && inspectItemVOS.length > 0){
                for(InspectItemVO inspectItemVO : inspectItemVOS){
                    userIdSet.add(inspectItemVO.getPk_rectificationer());
                }
            }

            // 制单人名称转换
            if(StringUtils.isNotEmpty(siteInspectionVO.getCreator())){
                userIdSet.add(siteInspectionVO.getCreator());
            }
        }

        if(userIdSet.size() <= 0){
            return;
        }

        Map<String, String> userInfoMap = new HashMap<>();
        UserInfo[] userInfoArray = userQuery.queryUsers(userIdSet.toArray(new String[]{}));
        if(userInfoArray == null || userInfoArray.length <= 0){
            return;
        }
        for(UserInfo userInfo : userInfoArray){
            userInfoMap.put(userInfo.getUserId(), userInfo.getUserName());
        }

        for(SiteInspectionVO siteInspectionVO : siteInspectionVOS){
            siteInspectionVO.setCreator_name(userInfoMap.get(siteInspectionVO.getCreator()));

            // 整改人名称转换
            InspectItemVO[] inspectItemVOS = siteInspectionVO.getInspectitems();
            if(inspectItemVOS != null && inspectItemVOS.length > 0){
                for(InspectItemVO inspectItemVO : inspectItemVOS){
                    inspectItemVO.setRectificationer_name(userInfoMap.get(inspectItemVO.getPk_rectificationer()));
                }
            }
        }
    }

}
